package com.zcxy360.sandtable.shiro;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

public class ExcessiveAttemptsHashedCredentialsMatcher extends HashedCredentialsMatcher {
	
/*	@Autowired
	private TaskScheduler taskScheduler;
	
	private Map<Object,Integer> attempts = new HashMap<>();
	private Map<Object,Long> waitTime= new HashMap<>();*/

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    	//关闭登陆次数过多限制
    	/*
    	Object principal = token.getPrincipal();
    	Integer count = attempts.get(principal);
    	if(count == null) {
    		attempts.put(principal, 1);
    	}else {
    		attempts.put(principal, count+1);
    	}
    	count = attempts.get(principal);
		if (count > 5) {
			if (count == 6) {
				waitTime.put(principal, System.currentTimeMillis());
				taskScheduler.schedule(new Runnable() {
					@Override
					public void run() {
						attempts.remove(principal);
					}
				}, new Date().toInstant().plusSeconds(60 * 10));
			}
			int waitMinute =(int) (10 - (System.currentTimeMillis()-waitTime.get(principal))/1000/60);
			throw new ExcessiveAttemptsException("尝试次数过多,请"+waitMinute+"分钟后再试");
		}*/
        Object tokenHashedCredentials = hashProvidedCredentials(token, info);
        Object accountCredentials = getCredentials(info);
        if(!equals(tokenHashedCredentials, accountCredentials)) {
        	throw new AccountException("密码错误");
        }/*else {
        	attempts.remove(principal);
        }*/
        return true;
    }
    
}
